package com.invite.me.ui.registeractivity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.invite.me.R;
import com.invite.me.base.BaseActivity;
import com.invite.me.beans.presenter.RegisterNumpresenter;
import com.invite.me.beans.res.YanZhenMaShouBean;
import com.invite.me.beans.res.ZheceBeanShou;
import com.invite.me.ui.home.HomeActivity;
import com.invite.me.util.PhoneFormatCheckUtils;
import com.invite.me.util.SspPrefUtils;
import com.invite.me.util.ToastUtils;
import com.invite.me.wigets.CountDownTextView;
import com.invite.me.wlqq.Constants;
import com.invite.me.wlqq.Contract;

import java.io.IOException;

public class RegisterNumberActivity extends BaseActivity implements View.OnClickListener,Contract.BaseView{


    private EditText zhuzephone;
    private EditText yanzhengma;
    private EditText shurumima;
    private CountDownTextView yanzhengmasend;
    RegisterNumpresenter registerNumpresenter;
    TextView zhuzhephone;
    TextView denglu;
    private ImageView yanjingkanjian;
    private boolean mimakanjian=false;
    private ImageView yanjingkanbujian;

    @Override
    protected void initView() {
        registerNumpresenter=new RegisterNumpresenter(this);
        LinearLayout xieyi = findViewById(R.id.xieyi);
        yanjingkanbujian = findViewById(R.id.yanjingkanbujian);
        zhuzephone = findViewById(R.id.zhuzephone);
        yanzhengma = findViewById(R.id.yanzhengma);
        shurumima = findViewById(R.id.shurumima);
        yanzhengmasend = findViewById(R.id.yanzhengmasend);
        zhuzhephone = findViewById(R.id.zhuzhephone);
        denglu = findViewById(R.id.denglu);
        yanjingkanjian = findViewById(R.id.yanjingkanjian);
        yanzhengmasend.setOnClickListener(this);
        zhuzhephone.setOnClickListener(this);
        denglu.setOnClickListener(this);
        yanjingkanjian.setOnClickListener(this);
        yanjingkanbujian.setOnClickListener(this);
        xieyi.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.yanjingkanjian:
                yanjingkanjian.setVisibility(View.GONE);
                yanjingkanbujian.setVisibility(View.VISIBLE);
                //      mimakanjian=true;//可见
                shurumima.setTransformationMethod(PasswordTransformationMethod.getInstance());
//                if(mimakanjian){
//
//                }else{
//
//                }



                break;
            case R.id.yanzhengmasend:
                //验证码
                String phones = zhuzephone.getText().toString();
                if(!TextUtils.isEmpty(phones)){
                    boolean phoneLegal = PhoneFormatCheckUtils.isPhoneLegal(phones);
                    if (phoneLegal) {
                        registerNumpresenter.yanzhengma(phones,"1");
                        yanzhengmasend.setCountDownEndText("重新发送")
                                .setCountingTextColor(
                                        ContextCompat.getColor(this, R.color.yanzhengmah)
                                )
                                .setCountEndTextColor(ContextCompat.getColor(this, R.color.yanzhengmah))
                                .setOnCountDownListener(new CountDownTextView.OnCountDownListener() {
                                    @Override
                                    public void onCountDown(int allTime, int remain) {
                                        if (remain <= 0) {
                                            yanzhengmasend.setClickable(true);
                                            yanzhengmasend.setEnabled(true);
                                        }
                                    }
                                })
                                .startCountDown();
                    }else {
                        ToastUtils.show(RegisterNumberActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT);

                    }

                }else {
                    ToastUtils.show(RegisterNumberActivity.this,"请输入手机号");
                }


                break;
            case R.id.zhuzhephone:
                String phonezhuce = zhuzephone.getText().toString();
                String yanzhengmazhuce = yanzhengma.getText().toString();
                String mimazc = shurumima.getText().toString();
                boolean phoneLegal = PhoneFormatCheckUtils.isPhoneLegal(phonezhuce);
                if (phoneLegal) {
                    if(!TextUtils.isEmpty(yanzhengmazhuce)&&yanzhengmazhuce.length()>=4){
                        if(!TextUtils.isEmpty(mimazc)&&mimazc.length()>6){
                            registerNumpresenter.zhucephone(phonezhuce,yanzhengmazhuce,mimazc);
                        }else {
                            ToastUtils.show(RegisterNumberActivity.this, "请输入正确的密码", Toast.LENGTH_SHORT);
                        }

                    }else {
                        ToastUtils.show(RegisterNumberActivity.this, "请输入正确的验证码", Toast.LENGTH_SHORT);

                    }


                }else {
                    ToastUtils.show(RegisterNumberActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT);

                }

                break;
            case R.id.denglu:

                Intent denglu=new Intent(RegisterNumberActivity.this,PasswordRegisterActivity.class);
                startActivity(denglu);
                finish();

                break;
            case R.id.xieyi:
                Intent xieyia=new Intent(RegisterNumberActivity.this,ProtocolActivity.class);

                startActivity(xieyia);



                break;
            case R.id.yanjingkanbujian:
                yanjingkanjian.setVisibility(View.VISIBLE);
                yanjingkanbujian.setVisibility(View.GONE);
                //        mimakanjian=false;//隐藏

                shurumima.setTransformationMethod(HideReturnsTransformationMethod.getInstance());


                break;

        }

    }
    @Override
    protected int getActivityLayoutId() {
        return R.layout.activity_registernumber;
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void setPresenter(Contract.BasePresenter basePresenter) {
        if (registerNumpresenter instanceof Contract.BasePresenter) {
            this.registerNumpresenter = (RegisterNumpresenter) basePresenter;
        }
    }

    @Override
    public void onDataSucess(Object o) throws IOException {
        if(o instanceof YanZhenMaShouBean){
            YanZhenMaShouBean yanZhenMaShouBean= (YanZhenMaShouBean) o;
            if(yanZhenMaShouBean.getCode()==2001){
                String id = yanZhenMaShouBean.getData().getUserId();
                SspPrefUtils.setString(Constants.SSPTOKEN,id);
                Intent denglu=new Intent(RegisterNumberActivity.this,PasswordRegisterActivity.class);
                startActivity(denglu);
                finish();
            }

        }else if(o instanceof ZheceBeanShou){
            ZheceBeanShou zheceBeanShou= (ZheceBeanShou) o;
            if(zheceBeanShou.getCode()==1000){
                ToastUtils.show(RegisterNumberActivity.this,zheceBeanShou.getMessage());
                String userId = zheceBeanShou.getData().getUserId();
                String nickname = zheceBeanShou.getData().getNickname();
                String headerImgPath = zheceBeanShou.getData().getHeaderImgPath();
                if(!TextUtils.isEmpty(nickname)){
                    SspPrefUtils.setString(Constants.NAKNAME,nickname);
                }
                if(!TextUtils.isEmpty(headerImgPath)){
                    SspPrefUtils.setString(Constants.TOUXIANG,headerImgPath);
                }


                SspPrefUtils.setString(Constants.SSPTOKEN,userId);
                Intent denglu=new Intent(RegisterNumberActivity.this,HomeActivity.class);
                startActivity(denglu);
                finish();


            }else {
                ToastUtils.show(RegisterNumberActivity.this,zheceBeanShou.getMessage());
            }

        }
    }

    @Override
    public void onDataFaile(Object o) {

    }

    @Override
    public void onComplete() {

    }
}
